<template>
  <el-row :gutter="20">
    <el-col :span="4" :xs="24">
      <ContentWrap class="h-1/1">
        <AppSiteSimpleList @row-click="handleAppSiteSimpleListRowClick" />
      </ContentWrap>
    </el-col>
    <el-col :span="20" :xs="24">
      <ContentWrap>
        <!-- 搜索工作栏 -->
        <el-form
          class="-mb-15px"
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="栏目标题" prop="title">
            <el-input
              v-model="queryParams.title"
              placeholder="请输入栏目标题"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
            />
          </el-form-item>
          <el-form-item label="副标题" prop="subTitle">
            <el-input
              v-model="queryParams.subTitle"
              placeholder="请输入副标题"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
            />
          </el-form-item>
          <el-form-item label="导航栏" prop="navBar">
            <el-select
              v-model="queryParams.navBar"
              placeholder="请选择导航栏"
              clearable
              class="!w-240px"
            >
              <el-option
                v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="栏目状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="请选择栏目状态"
              clearable
              class="!w-240px"
            >
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="handleQuery">
              <Icon icon="ep:search" class="mr-5px" />
              搜索
            </el-button>
            <el-button @click="resetQuery">
              <Icon icon="ep:refresh" class="mr-5px" />
              重置
            </el-button>
            <el-button
              type="primary"
              plain
              @click="openForm('create', queryParams.appSiteId, queryParams.appId)"
              v-hasPermi="['cms:article-category:create']"
              v-if="queryParams.appSiteId && queryParams.appId"
            >
              <Icon icon="ep:plus" class="mr-5px" />
              新增
            </el-button>
            <el-button
              type="success"
              plain
              @click="handleExport"
              :loading="exportLoading"
              v-hasPermi="['cms:article-category:export']"
            >
              <Icon icon="ep:download" class="mr-5px" />
              导出
            </el-button>
            <el-button type="danger" plain @click="toggleExpandAll">
              <Icon icon="ep:sort" class="mr-5px" />
              展开/折叠
            </el-button>
          </el-form-item>
        </el-form>
      </ContentWrap>

      <!-- 列表 -->
      <ContentWrap>
        <el-table
          v-loading="loading"
          :data="list"
          :stripe="true"
          row-key="id"
          :default-expand-all="isExpandAll"
          v-if="refreshTable"
          :show-overflow-tooltip="true"
        >
          <el-table-column label="栏目标题" align="center" prop="title" />
          <el-table-column label="副标题" align="center" prop="subTitle" />
          <el-table-column label="图标" align="center" prop="iconUrl">
            <template #default="scope">
              <el-image
                class="h-60px max-w-60px"
                v-if="scope.row.iconUrl"
                :key="scope.row.iconUrl"
                :src="scope.row.iconUrl"
                :preview-src-list="[scope.row.iconUrl]"
                :initial-index="0"
                preview-teleported
              />
            </template>
          </el-table-column>
          <el-table-column label="导航栏" align="center" prop="navBar">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.navBar" />
            </template>
          </el-table-column>
          <el-table-column label="栏目状态" align="center" prop="status">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="点击量" align="center" prop="viewNum" />
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            :formatter="dateFormatter"
            width="180px"
          />
          <el-table-column label="操作" align="center">
            <template #default="scope">
              <el-button
                link
                type="primary"
                @click="openForm('update', scope.row.appSiteId, scope.row.appId, scope.row.id)"
                v-hasPermi="['cms:article-category:update']"
              >
                编辑
              </el-button>
              <el-button
                link
                type="danger"
                @click="handleDelete(scope.row.id)"
                v-hasPermi="['cms:article-category:delete']"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </ContentWrap>
    </el-col>
  </el-row>
  <!-- 表单弹窗：添加/修改 -->
  <ArticleCategoryForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { DICT_TYPE, getBoolDictOptions, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ArticleCategoryApi, ArticleCategoryVO } from '@/api/cms/articlecategory'
import ArticleCategoryForm from './ArticleCategoryForm.vue'
import AppSiteSimpleList from '@/views/cms/appsite/AppSiteSimpleList.vue'
import { AppSiteVO } from '@/api/cms/appsite'
import { handleTree } from '@/utils/tree'

/** 文章栏目 列表 */
defineOptions({ name: 'ArticleCategory' })

const isExpandAll = ref(true) // 是否展开，默认全部展开
const refreshTable = ref(true) // 重新渲染表格状态
/** 展开/折叠操作 */
const toggleExpandAll = () => {
  refreshTable.value = false
  isExpandAll.value = !isExpandAll.value
  nextTick(() => {
    refreshTable.value = true
  })
}

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ArticleCategoryVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: -1,
  appSiteId: 0,
  appId: '',
  parentId: undefined,
  title: undefined,
  subTitle: undefined,
  navBar: undefined,
  status: undefined,
  viewNum: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ArticleCategoryApi.getArticleCategoryPage(queryParams)
    list.value = handleTree(data.list)
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const handleAppSiteSimpleListRowClick = (row: AppSiteVO) => {
  queryParams.appSiteId = row.id
  queryParams.appId = row.appId
  handleQuery()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, appSiteId: number, appId: string, id?: number) => {
  formRef.value.open(type, appSiteId, appId, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ArticleCategoryApi.deleteArticleCategory(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ArticleCategoryApi.exportArticleCategory(queryParams)
    download.excel(data, '文章栏目.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
